/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cardgame;

import cardgame.game.*;
import cardgame.game.bots.*;
import cardgame.game.modes.*;
import cardgame.gui.*;
import cardgame.network.*;
import cardgame.network.messages.*;
import com.esotericsoftware.kryonet.*;

/**
 *
 * @author Carl
 */
public class PanPlay extends javax.swing.JPanel{

    public PanPlay(final Client client, Deck deck){
        initComponents();
        
        panCardInfo = new PanCardInfo();
        panCardContainer.add(panCardInfo);
        panMessage = new PanMessage();
        panMessageContainer.add(panMessage);
        
        final CommandSender commandSender = new CommandSender(){

            @Override
            public void sendCommand(Command command){
                client.sendTCP(new Message_Command(command));
            }
        };
        client.addListener(new Listener(){

            @Override
            public void received(Connection connection, Object receivedMessage){
                if(receivedMessage instanceof Message_ClientAccepted){
                    Message_ClientAccepted message = (Message_ClientAccepted) receivedMessage;
                    GameMode gameMode = Util.createObjectByClassName(message.getGameModeClassName(), GameMode.class);
                    game.setGameMode(gameMode);
                    panGame.setPlayerIndex(message.getPlayerIndex());
                    guiTest.getPanField().setPlayerIndex(message.getPlayerIndex());
                    guiTest.getPanField().setBot(manualBot);
                    for(int i=0;i<bots.length;i++){
                        if(bots[i] != null){
                            bots[i].initialize(game, commandSender);
                        }
                    }
                }
                else if(receivedMessage instanceof Message_GameUpdate){
                    Message_GameUpdate message = (Message_GameUpdate) receivedMessage;
                    NetworkUtil.unserialize(game, message.getData());
                    java.awt.EventQueue.invokeLater(new Runnable(){

                        @Override
                        public void run(){
                            panCardInfo.update();
                            panGame.update();
                        }
                    });
                    if((game.getCurrentPlayerIndex() != tmpCurrentPlayerIndex) && (game.getCurrentPlayerIndex() == panGame.getPlayerIndex())){
                        final Bot bot = bots[panGame.getPlayerIndex()];
                        if(bot != null){
                            new Thread(new Runnable(){

                                @Override
                                public void run(){
                                    try{
                                        Thread.sleep(botTurnDelay);
                                    }catch(InterruptedException ex){
                                    }
                                    bot.performTurn();
                                }
                            }).start();
                        }
                    }
                    tmpCurrentPlayerIndex = game.getCurrentPlayerIndex();
                }
                else if(receivedMessage instanceof Message_GameOver){
                    Message_GameOver message = (Message_GameOver) receivedMessage;
                    boolean isVictory = (message.getWinnerPlayerIndex() == panGame.getPlayerIndex());
                    FrameUtil.showMessageDialog(PanPlay.this, (isVictory?"Victory!":"Defeat!"), FrameUtil.MessageType.INFORMATION);
                }
            }
        });
        
        game = new Game(false);
        game.setPlayers(new Player(), new Player());
        panGame = new PanGame(game, commandSender, panCardInfo, panMessage);
        panGameContainer.add(panGame);
        
        guiTest = new GUITest(commandSender);
        guiTest.getPanField().setGame(game);
        guiTest.setVisible(true);
        
        client.sendTCP(new Message_ClientInitialized(NetworkUtil.serialize(deck.getPlainTypeSerializer())));
    }
    private PanCardInfo panCardInfo;
    private PanMessage panMessage;
    private PanGame panGame;
    private GUITest guiTest;
    private Game game;
    private int tmpCurrentPlayerIndex = -1;
    public static final GameMode GAME_MODE = new DestroMode();
    private Bot manualBot = new IntelligentBot();
    private Bot[] bots = new Bot[]{
        null,//new Etherbot(20),
        null//new IntelligentBot()
    };
    private int botTurnDelay = 200;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panGameContainer = new javax.swing.JPanel();
        panMessageContainer = new javax.swing.JPanel();
        panCardContainer = new javax.swing.JPanel();

        panGameContainer.setLayout(new java.awt.GridLayout(1, 0));

        panMessageContainer.setLayout(new java.awt.GridLayout(1, 0));

        panCardContainer.setLayout(new java.awt.GridLayout(1, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(panCardContainer, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                    .addComponent(panMessageContainer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panGameContainer, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panGameContainer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 242, Short.MAX_VALUE)
                .addComponent(panMessageContainer, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panCardContainer, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel panCardContainer;
    private javax.swing.JPanel panGameContainer;
    private javax.swing.JPanel panMessageContainer;
    // End of variables declaration//GEN-END:variables
}
